package o2o.service.statis;


import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.VO.statis.PostManOrderStatisVO;
import org.apache.commons.lang.StringUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;

/**
 * @author Jahnke【1029777564@qq.com】
 * @ClassName PostManOrderStatisService
 * @Description: 配送员订单统计信息
 * @date 2019/8/23 20:39
 * @Version 1.0
 */
public class PostManOrderStatisService {

    private PostManOrderStatisService() {
    }

    enum Singleton {
        INSTANCE;
        private PostManOrderStatisService single;

        private Singleton() {
            single = new PostManOrderStatisService();
        }

        private PostManOrderStatisService getInstance() {
            return single;
        }
    }

    public static PostManOrderStatisService getInstance() {
        return Singleton.INSTANCE.single;
    }

    // 统计配送员的订单数量等信息
    public List<PostManOrderStatisVO> postManOrderStatis(Long companyId, Set<String> usernames, Long startTime, Long endTime) {

        /**
         * SELECT ofo.postmanUsername,COUNT(*) orderCounts,0 AS timeoutCount,0 AS abnormalCount,
         * 	SUM(opy.postmanmoney) postmanmoney,SUM(IFNULL(opy.plateAllowance,0)) plateAllowance,SUM(IFNULL(opy.companyAllowance,0)) companyAllowance
         * FROM  order_info AS ofo
         * LEFT JOIN order_pay AS opy ON opy.orderno=ofo.orderno
         * WHERE ofo.delFlag=0 AND ofo.orderstatus IN(9,19,64) AND ofo.isRefundApply!=2
         * GROUP BY ofo.postmanUsername
         */
        String sql = "SELECT ofo.postmanUsername,COUNT(*) orderCounts,0 AS timeoutCount,0 AS abnormalCount," +
                " SUM(opy.postmanmoney) postmanmoney,SUM(IFNULL(opy.plateAllowance,0)) plateAllowance,SUM(IFNULL(opy.companyAllowance,0)) companyAllowance " +
                " FROM  order_info AS ofo " +
                " LEFT JOIN order_pay AS opy ON opy.orderno=ofo.orderno " +
                " WHERE ofo.delFlag=0 AND ofo.orderstatus IN(" + (StringUtils.join(ORDER_STATUS.finishDeliverSet, ",")) + ") AND ofo.isRefundApply!=2 " +
                " and ofo.postmanUsername in('" + (StringUtils.join(usernames, "','")) + "')" +
                (companyId != null && companyId > 0 ? " and opy.postcompanyId=" + companyId : "") +
                (startTime != null && startTime > 0 ? "ofo.postmanFinishTime>=" + startTime : "") +
                (endTime != null && endTime > 0 ? "ofo.postmanFinishTime<=" + endTime : "") +
                " GROUP BY ofo.postmanUsername";

        List<PostManOrderStatisVO> list = null;
        try {
            list = (List<PostManOrderStatisVO>) UserDaoSql.getInstance().list(PostManOrderStatisVO.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

}
